﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MealBooking.Core;
using ThisIsFirstTest.Filter;
using MealBooking.Core.Models.Accounts;
using MealBooking.Core.Models.Orders;
using MealBooking.Core.Models.Meals;

namespace ThisIsFirstTest.Areas.Manager.Controllers
{
    [AccountAuthorize(AccountRoles = Role.Manager)]
    public class OrderManagerController : Controller
    {
        public ActionResult Index()
        {
            var managerTodayOrder = GetManagerTodayOrder();
            return View(managerTodayOrder);
        }
        public ActionResult TodayOrder(string headerMessage = null)
        {
            ViewBag.HeaderTitle = headerMessage + "Today Order";
            var managerTodayOrder = GetManagerTodayOrder();
            return View(managerTodayOrder);
        }
        [HttpPost]
        public ActionResult BookTodayOrder()
        {
            var managerTodayOrder = GetManagerTodayOrder();

            if (managerTodayOrder == null)
            {
                return RedirectToAction("TodayOrder", new { headerMessage = "No Orders yet." });
            }
            if (managerTodayOrder.IsBooked)
            {
                return RedirectToAction("TodayOrder", new { headerMessage = "Already Booked." });
            }
            //update information:
            CoreService.OrderService.BookManagerOrder(User.Identity.Name);
            return RedirectToAction("TodayOrder", new { headerMessage = "Book Success." });
        }

        #region All Customers' Today Orders
        public ActionResult TodayOrders(string headerMessage = null)
        {
            ViewBag.HeaderTitle = headerMessage + "Order Details";
            var orders = CoreService.OrderService.TodayOrdersInManagerOrder(User.Identity.Name);
            return View(orders);
        }
        #endregion

        public ActionResult UserOrderDetail(string id, string headerMessage = null)
        {
            ViewBag.HeaderTitle = headerMessage + "Order Detail";
            var order = CoreService.OrderService.OrderDetail(id);
            return View(order);
        }
        private ManagerOrder GetManagerTodayOrder()
        {
            string accountId = User.Identity.Name;
            var managerTodayOrder = CoreService.OrderService.ManagerTodayOrder(accountId);
            return managerTodayOrder;
        }
    }
}
